//
//  CrmSortView.m
//  New_Runway
//
//  Created by mack on 2018/3/2.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "CrmSortView.h"
#import "CrmSortCell.h"
@interface CrmSortView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableview;
@end
@implementation CrmSortView
{
    NSIndexPath *_selIndex;
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self == [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0];
        [self backImageview];
        [self initTableview];
    }
    return self;
}
-(void)backImageview{
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    backImage.image = [UIImage imageNamed:@"sortBack"];
    [self addSubview:backImage];
}
-(void)initTableview{
    //    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        //        self.automaticallyAdjustsScrollViewInsets = false;
    }
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"CrmSortCell" bundle:nil] forCellReuseIdentifier:@"CrmSortCell"];
    [self addSubview:self.tableview];
    
    self.tableview.frame = CGRectMake(0, 7, self.frame.size.width, self.frame.size.height);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 35;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *images = @[@"ASC_N",@"DESC_N"];
    NSArray *titles = @[@"升序",@"降序"];
    CrmSortCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CrmSortCell"];
    cell.backgroundColor = [UIColor clearColor];
    [cell.sortButton setTitle:titles[indexPath.row] forState:UIControlStateNormal];
    [cell.sortButton setImage:images[indexPath.row] forState:UIControlStateNormal];
    if (indexPath.row == 1) {
        UILabel *line = [[UILabel alloc] init];
        line.backgroundColor = kColorWhite();
        line.frame = CGRectMake(0, 0, Screen_width, 0.5);
        [cell.contentView addSubview:line];
    }
    
    if (_selIndex == indexPath) {
        [cell.sortButton setTitleColor:MAIN_NAV_COLOR() forState:UIControlStateNormal];
        if (indexPath.row == 0) {
            [cell.sortButton setImage:[UIImage imageNamed:@"ASC_S"] forState:UIControlStateNormal];
        }else{
            [cell.sortButton setImage:[UIImage imageNamed:@"DESC_S"] forState:UIControlStateNormal];
            
        }
    }else {
        [cell.sortButton setTitleColor:kColorWhite() forState:UIControlStateNormal];
        if (indexPath.row == 0) {
            [cell.sortButton setImage:[UIImage imageNamed:@"ASC_N"] forState:UIControlStateNormal];
        }else{
            [cell.sortButton setImage:[UIImage imageNamed:@"DESC_N"] forState:UIControlStateNormal];
            
        }
        
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CrmSortCell *celled = [tableView cellForRowAtIndexPath:_selIndex];
    
    [celled.sortButton setTitleColor:kColorWhite() forState:UIControlStateNormal];
    if (_selIndex.row == 0) {
        [celled.sortButton setImage:[UIImage imageNamed:@"ASC_N"] forState:UIControlStateNormal];
    }else{
        [celled.sortButton setImage:[UIImage imageNamed:@"DESC_N"] forState:UIControlStateNormal];

    }
    
    //记录当前选中的位置索引
    _selIndex = indexPath;
    //当前选择的打勾
    CrmSortCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell.sortButton setTitleColor:MAIN_NAV_COLOR() forState:UIControlStateNormal];
    if (_selIndex.row == 0) {
        [cell.sortButton setImage:[UIImage imageNamed:@"ASC_S"] forState:UIControlStateNormal];
        self.sortBlock(YES);
    }else{
        [cell.sortButton setImage:[UIImage imageNamed:@"DESC_S"] forState:UIControlStateNormal];
        self.sortBlock(NO);
    }
    
    
    
}
@end
